Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds condition to check XDG_DATA_HOME to set global_dir value #2361

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from

Conversation

goosethedev
Copy link

Description

Check for XDG_DATA_HOME env var when setting the global directory for storing pipeline data.

If the variable if set but ~/.dlt exists, then it is used with a warning raised to move it manually and not break backwards compatibility.

Related Issues

Additional Context

Copy link

netlify bot commented Feb 28, 2025

Deploy Preview for dlt-hub-docs canceled.

Name Link
🔨 Latest commit ae5c493
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/67c23a016c679c000828f450

Copy link
Collaborator

@sh-rp sh-rp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution :) I have one note, and it would be good to have a test for this. maybe in test_run_context.py

if not os.path.isdir(os.path.join(home, DOT_DLT)):
return os.path.join(os.environ["XDG_DATA_HOME"], "dlt")
else:
warnings.warn(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use logger.warning in our codebase, maybe use that instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean dlt.common.logger, when I import it into run_context.py the conftest fails:

ImportError while loading conftest 'tests/common/runtime/conftest.py'.
tests/common/runtime/conftest.py:1: in <module>
    from tests.utils import preserve_environ
tests/utils.py:43: in <module>
    ALL_DESTINATIONS = dlt.config.get("ALL_DESTINATIONS", list) or [
dlt/common/configuration/accessors.py:39: in get
    value, _ = self._get_value(field, expected_type)
dlt/common/configuration/accessors.py:74: in _get_value
    for provider in self.config_providers:
dlt/common/configuration/accessors.py:104: in config_providers
    return [p for p in self._get_providers_from_context()]
dlt/common/configuration/accessors.py:64: in _get_providers_from_context
    return Container()[PluggableRunContext].providers.providers
dlt/common/configuration/container.py:67: in __getitem__
    item = spec()
dlt/common/configuration/specs/pluggable_run_context.py:90: in __init__
    self._plug(run_dir=None)
dlt/common/configuration/specs/pluggable_run_context.py:161: in _plug
    m = plugins.manager()
dlt/common/configuration/plugins.py:47: in manager
    return Container()[PluginContext].manager
dlt/common/configuration/container.py:67: in __getitem__
    item = spec()
dlt/common/configuration/plugins.py:31: in __init__
    self.manager.add_hookspecs(run_context)
E   AttributeError: 'function' object has no attribute 'get'

Without importing it, the tests run normally.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! let's keep the warn. When creating a run_context, logger may not be yet available... Let's skip this for now

@goosethedev
Copy link
Author

For the test, for now I have this test passing in test_run_context.py:

def test_context_with_xdg_dir() -> None:
    import tempfile
    temp_data_home = os.path.join(tempfile.gettempdir(), "test")

    os.environ["XDG_DATA_HOME"] = temp_data_home

    ctx = PluggableRunContext()
    run_context = ctx.context
    assert run_context.global_dir == os.path.join(temp_data_home, "dlt")

    os.environ.pop("XDG_DATA_HOME")

As for the failing CI above, it seems I have to do if "XDG_DATA_HOME" in os.environ to avoid a KeyError. I'll fix it and rerun tests locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Follow the XDG Base Directory spec for local pipelines' data
3 participants